package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallStorage;
import com.ctsi.ssdc.domain.LitemallStorageExample;
import com.ctsi.ssdc.service.LitemallStorageService;
import com.ctsi.ssdc.repository.LitemallStorageRepository;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallStorage.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallStorageServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallStorageRepository, LitemallStorage, Integer, LitemallStorageExample> 
	implements LitemallStorageService {

    @Autowired
    LitemallStorageRepository litemallStorageRepository;


    /**
    * GET  /litemallStorages : get the litemallStorages firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallStorage> findFirstStringColumn(String key,Pageable pageable){
        String str = key;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallStorageExample litemallStorageExample = new LitemallStorageExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallStorageExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallStorageExample.createCriteria();
        } else{
            litemallStorageExample.createCriteria().andKeyLike("%" + str +"%");
        }
        List<LitemallStorage>  data = litemallStorageRepository.selectByExample(litemallStorageExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallStorageRepository.countByExample(litemallStorageExample);
        }
        return new PageResult<LitemallStorage>(data,count,count);
    }

    @Override
    public void add(LitemallStorage storageInfo) {
        storageInfo.setAddTime(ZonedDateTime.now());
        storageInfo.setUpdateTime(ZonedDateTime.now());
        litemallStorageRepository.insertSelective(storageInfo);
    }

    @Override
    public LitemallStorage findByKey(String key) {
        LitemallStorageExample example = new LitemallStorageExample();
        example.or().andKeyEqualTo(key).andDeletedEqualTo(false);
        return litemallStorageRepository.selectOneByExample(example);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
